package com.claimSettlement.controller;

import com.claimSettlement.pojo.*;
import com.claimSettlement.service.SurveyLossOpenFeginService;
import com.claimSettlement.service.UserService;
import com.github.pagehelper.PageInfo;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 访问查勘定损微服务控制层
 * 曹文韬
 */
@RestController
@RequestMapping("/user")
public class ProspectDamagesController {
    @Resource
    SurveyLossOpenFeginService surveyLossOpenFeginService;

    @Resource
    UserService userService;

    @RequestMapping("/getSurveyLossIdOrCarframeId")
    public PageInfo<SurveyLoss> getSurveyLossIdOrCarframeId(int id,String carframeId,int page){
        return surveyLossOpenFeginService.getSurveyLossIdOrCarframeId(id, carframeId, page);
    }

    @RequestMapping(value = "/insSurveyLoss", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String insSurveyLoss(@RequestBody SurveyLoss surveyLoss){
        return surveyLossOpenFeginService.insSurveyLoss(surveyLoss);
    }

    @RequestMapping(value = "/delSurveyLoss",produces = "text/html;charset=UTF-8")
    public String delSurveyLoss(String caseId){
        surveyLossOpenFeginService.delLossDetermination(caseId);
        return surveyLossOpenFeginService.delSurveyLoss(caseId);
    }

    @RequestMapping(value = "/updSurveyLossProspectState", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String updSurveyLossProspectState(@RequestBody SurveyLoss surveyLoss){
        return surveyLossOpenFeginService.updSurveyLossProspectState(surveyLoss);
    }

    @RequestMapping(value = "/insLossDetermination", method = RequestMethod.POST)
    public int insLossDetermination(@RequestBody LossDetermination lossDetermination){
        return surveyLossOpenFeginService.insLossDetermination(lossDetermination);
    }
    @RequestMapping("/getLossDeterminationByUserId")
    public LossDetermination getLossDeterminationByUserId(String caseId){
        return surveyLossOpenFeginService.getLossDeterminationByUserId(caseId);
    }

    @RequestMapping(value = "/updLossDetermination", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String updLossDetermination(@RequestBody LossDetermination lossDetermination){
        return surveyLossOpenFeginService.updLossDetermination(lossDetermination);
    }

    @RequestMapping(value = "/uploadImgs", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String uploadImgs(@RequestParam("uploadimg") MultipartFile uploadimg){
        /*return surveyLossOpenFeginService.uploadImgs(uploadimg);*/
        //创建客户端对象
        String path = "http://localhost:7777/fileupload_server_7777_war_exploded/imges/";
        Client client=Client.create();
        WebResource webResource = null;
        try {
            //for (Map.Entry<String,File> entry:map.entrySet()){
            //MultipartFile multipartFile = FileUtil.fileToMultipartFile(uploadimg);
            if(uploadimg.getOriginalFilename().length() == 0 || uploadimg.getOriginalFilename() == null)return "";
            String fileName = UUID.randomUUID().toString().substring(0,18)+"-" + uploadimg.getOriginalFilename();
            //创建客户端对象
            webResource=client.resource(path+fileName);
            //上传文件到图片服务器
            webResource.put(uploadimg.getBytes());
            //}
        }catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return "成功";
    }

    @RequestMapping(value = "/updSurveyLossSettUserId", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    String updSurveyLossSettUserId(@RequestBody SurveyLoss surveyLoss){
        userService.updUserState(surveyLoss.getSettUserId());
        return surveyLossOpenFeginService.updSurveyLossSettUserId(surveyLoss);
    }

    @RequestMapping(value = "/insCheLoss", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    String insCheLoss(@RequestBody CheLoss cheLoss){
        return surveyLossOpenFeginService.insCheLoss(cheLoss);
    }

    @RequestMapping(value = "/insWuLoss", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    String insWuLoss(@RequestBody WuLoss wuLoss){
        return surveyLossOpenFeginService.insWuLoss(wuLoss);
    }

    @RequestMapping(value = "/insRenLoss", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    String insRenLoss(@RequestBody RenLoss renLoss){
        return surveyLossOpenFeginService.insRenLoss(renLoss);
    }

    @RequestMapping("/getCheLoss")
    List<CheLoss> getCheLoss(String caseId){
        return surveyLossOpenFeginService.getCheLoss(caseId);
    }

    @RequestMapping("/getWuLoss")
    List<WuLoss> getWuLoss(String caseId){
        return surveyLossOpenFeginService.getWuLoss(caseId);
    }

    @RequestMapping("/getRenLoss")
    List<RenLoss> getRenLoss(String caseId){
        return surveyLossOpenFeginService.getRenLoss(caseId);
    }

    @RequestMapping(value = "/updCheLoss", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String updCheLoss(@RequestBody CheLoss cheLoss) {
        return surveyLossOpenFeginService.updCheLoss(cheLoss);
    }

    @RequestMapping(value = "/updWuLoss", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String updWuLoss(@RequestBody WuLoss wuLoss){
        return surveyLossOpenFeginService.updWuLoss(wuLoss);
    }

    @RequestMapping(value = "/updRenLoss", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String updRenLoss(@RequestBody RenLoss renLoss){
        return surveyLossOpenFeginService.updRenLoss(renLoss);
    }

    @RequestMapping("/getLossDataAll")
    public List<LossDetermination> getLossDataAll(){
        return surveyLossOpenFeginService.getLossDataAll();
    }
}
